# Copyright 2021 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

CHANGES = [
    # both
    ("ch9", "ch11"),

    # train_on_vertexai.py
    ("ENDPOINT_NAME = 'flights'", "ENDPOINT_NAME = 'flights-ch11'"),

    # model.py
    ("arr_airport_lat,arr_airport_lon", "arr_airport_lat,arr_airport_lon,avg_dep_delay,avg_taxi_out"),
    ("43.41694444, -124.24694444, 39.86166667, -104.67305556, 'TRAIN'",
     "43.41694444, -124.24694444, 39.86166667, -104.67305556, -3.0, 5.0, 'TRAIN'"),

    # call_predict.py
    ('"carrier": "AS"', '"carrier": "AS", "avg_dep_delay": -3.0, "avg_taxi_out": 5.0'),
    ('"carrier": "HA"', '"carrier": "HA", "avg_dep_delay": 3.0, "avg_taxi_out": 8.0'),
]

for filename in ['train_on_vertexai.py', 'model.py', 'call_predict.py']:
    in_filename = os.path.join('../10_mlops', filename)
    with open(in_filename, "r") as ifp:
        with open(filename, "w") as ofp:
            ofp.write("#### DO NOT EDIT! Autogenerated from {}".format(in_filename))
            for line in ifp.readlines():
                for change in CHANGES:
                    new_line = line.replace(change[0], change[1])
                    if new_line != line:
                        print('<<' + line + '>>' + new_line)
                        line = new_line
                ofp.write(line)

    print("*** Wrote out {}".format(filename))
